package com.sjzsqjy.asdtiang.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import com.opensymphony.xwork2.ActionSupport;
import com.sjzsqjy.asdtiang.db.ShareService;
import com.sjzsqjy.asdtiang.mysql.FeedBack;
import com.sjzsqjy.asdtiang.mysql.Info;
import com.sjzsqjy.asdtiang.mysql.InfoType;

public class ObjectAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2977733219394540288L;
	private Object object;
	private String objectName;
	private String id;
	private List listAll;
	private ShareService shareService;
	private FeedBack feedBack;
	
	
	public FeedBack getFeedBack() {
		return feedBack;
	}
	public void setFeedBack(FeedBack feedBack) {
		this.feedBack = feedBack;
	}
	public Object getObject() {
		return object;
	}
	public void setObject(Object object) {
		this.object = object;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public ShareService getShareService() {
		return shareService;
	}
	public void setShareService(ShareService shareService) {
		this.shareService = shareService;
	}
	
	public String showExecute() throws Exception {
		object=shareService.getObjectById(objectName,id);
		if(objectName.equals("InfoType")){
			List l=new ArrayList(((InfoType)object).getInfos());
			Collections.sort(l, new Comparator() {
				public int compare(Object o1, Object o2) {
					// TODO Auto-generated method stub
					return (int) (((Info) o2).getDateCreated().getTime()
							- ((Info) o1).getDateCreated().getTime());
				}
			});
			System.out.println("llllllllll");
			Set set = new LinkedHashSet(l);
			((InfoType)object).setInfos(set);
		}
		//System.out.println("showExecute:"+"show"+objectName);
		return "show"+objectName;
	}
	
	
	public String listAllExecute() throws Exception {
		String order="";
		listAll=shareService.listAllByObjectName(objectName,order);
		//System.out.println("show List");
		return "list"+objectName;
	}
	public String createExecute() throws Exception {
		if(null!=objectName&&objectName.equals("feedBack")){
			feedBack=new FeedBack();
		}
		return "create"+objectName;
	}
	public String saveExecute() throws Exception {
		if(objectName.equals("FeedBack")){
			//System.out.println(object.getClass().getName());
			object=FeedBack.validate(feedBack, this);
			if(null==object){
				this.addFieldError("null",this.getText("toUsEmpty"));
				return INPUT+objectName;
			}
		    feedBack.setReply(this.getText("feedBack.ok"));
			shareService.save(object);
		}	
		return "save"+objectName;
	}
	
	public List getListAll() {
		return listAll;
	}
	public void setListAll(List listAll) {
		this.listAll = listAll;
	}
	public String getObjectName() {
		return objectName;
	}
	public void setObjectName(String objectName) {
		this.objectName = objectName;
	}
	
	
	

}
